//
//  AddressViewController.m
//  FruitVIP
//
//  Created by 孙艳玲 on 15/5/21.
//  Copyright (c) 2015年 孙艳玲. All rights reserved.
//

#import "AddressViewController.h"

#import "FirstPageViewController.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface AddressViewController ()

@end

@implementation AddressViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;

}
- (void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
;
    self.navigationItem.title = @"提货点";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255.0/255.0 green:206.0 / 255.0 blue:0 / 255.0 alpha:1];
 [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1]}];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    backImage.contentMode = UIViewContentModeScaleAspectFit;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 5, 10, 20);
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [backImage addSubview:button];
    UITapGestureRecognizer *backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftButtonAction)];
    [backImage addGestureRecognizer:backTap];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backImage];
    
    
    
    
        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH/4 , SCREEN_HEIGHT- 64 - 100)];
        self.myTableView.delegate = self;
        self.myTableView.dataSource = self;
       self.myTableView.tag = 1001;
       self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       [self.view addSubview:self.myTableView];
    
      UILabel *firstLine = [[UILabel alloc] initWithFrame:CGRectMake(self.myTableView.frame.origin.x + self.myTableView.frame.size.width, self.myTableView.frame.origin.y, 1, self.myTableView.frame.size.height)];
    firstLine.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    [self.view addSubview:firstLine];
    
    UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(firstLine.frame.origin.x + firstLine.frame.size.width, self.myTableView.frame.origin.y, SCREEN_WIDTH - self.myTableView.frame.size.width - firstLine.frame.size.width, 50)];
    headView.userInteractionEnabled = YES;
    UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headTapAction)];
    [headView addGestureRecognizer:headTap];
    [self.view addSubview:headView];
    
    UILabel *selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headView.frame.size.width/3, headView.frame.size.height)];
    selectLabel.text = @"选择商圈";
    selectLabel.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:selectLabel];
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(selectLabel.frame.origin.x + selectLabel.frame.size.width + 20, 0, headView.frame.size.width/2 - 10, headView.frame.size.height)];
    detailLabel.textAlignment = NSTextAlignmentCenter;
    detailLabel.text = @"上海动物园";
    [headView addSubview:detailLabel];
    
    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    selectButton.frame = CGRectMake(detailLabel.frame.origin.x + detailLabel.frame.size.width, headView.frame.size.height/2-5, 15, 10);
    [selectButton addTarget:self action:@selector(selectButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [selectButton setImage:[UIImage imageNamed:@"countButton.png"] forState:UIControlStateNormal];
    [headView addSubview:selectButton];
    
    UILabel *secondLine = [[UILabel alloc] initWithFrame:CGRectMake(headView.frame.origin.x, headView.frame.origin.y + headView.frame.size.height, headView.frame.size.width, 1)];
    secondLine.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    [self.view addSubview:secondLine];
    
    self.detailTableView = [[UITableView alloc] initWithFrame:CGRectMake(secondLine.frame.origin.x, secondLine.frame.origin.y + secondLine.frame.size.height, headView.frame.size.width, self.myTableView.frame.size.height - headView.frame.size.height) style:UITableViewStylePlain];
    self.detailTableView.tag = 1002;
    self.detailTableView.delegate = self;
    self.detailTableView.dataSource = self;
    self.detailTableView.separatorColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    [self.view addSubview:self.detailTableView];
    
    
    self.myImageView = [[UIImageView alloc] init];
    self.myImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT  - 49 );
    self.myImageView.userInteractionEnabled = YES;
    self.myImageView.backgroundColor = [UIColor colorWithRed:109/255.0 green:109/255.0 blue:109/255.0 alpha:0.5];
    self.myImageView.hidden = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.myImageView addGestureRecognizer:tap];
    [self.view addSubview:self.myImageView];
    
    
    
    self.alertTableView = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/8, SCREEN_HEIGHT/8, 6 * SCREEN_WIDTH/8, 5 * SCREEN_HEIGHT/8) style:UITableViewStylePlain];
    self.alertTableView.delegate = self;
    self.alertTableView.dataSource = self;
    self.alertTableView.tag = 1003;
    self.alertTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.alertTableView.layer.cornerRadius = CGRectGetHeight([self.alertTableView bounds])/20;
    [self.myImageView addSubview:self.alertTableView];

    
    
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 50)];
    searchView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:206.0 / 255.0 blue:0 / 255.0 alpha:1];
    [self.view addSubview:searchView];
    
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH/2, 30)];
        searchBar.backgroundColor = [UIColor whiteColor];
        searchBar.placeholder = @"请输入地址";
     searchBar.layer.masksToBounds = YES;
    searchBar.layer.cornerRadius = 5;
    searchBar.backgroundColor = [UIColor clearColor];
     [searchView addSubview:searchBar];

    
    UIButton *seachButton = [UIButton buttonWithType:UIButtonTypeCustom];
    seachButton.frame = CGRectMake(searchBar.frame.origin.x + searchBar.frame.size.width, searchBar.frame.origin.y, 50, searchBar.frame.size.height);
    seachButton.backgroundColor = [UIColor colorWithRed:109.0/255.0 green:77.0 / 255.0 blue:69 / 255.0 alpha:1];
    [seachButton setTitle:@"搜索" forState:UIControlStateNormal];
    [seachButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    seachButton.titleLabel.font = [UIFont systemFontOfSize:17];
    seachButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [searchView addSubview:seachButton];
    
    UIImageView *addImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, searchView.frame.origin.y + searchView.frame.size.height +15,25, 25)];
    addImageView.contentMode = UIViewContentModeScaleAspectFit;
    UIImage *addImage = [UIImage imageNamed:@"address.png"];
    addImageView.image = addImage;
    [self.view addSubview:addImageView];
    
    UILabel *addLine = [[UILabel alloc] initWithFrame:CGRectMake(0, self.myTableView.frame.origin.y, SCREEN_WIDTH, 1)];
    addLine.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
    [self.view addSubview:addLine];
    
    UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(addImageView.frame.origin.x + addImageView.frame.size.width + 10, searchView.frame.origin.y + searchView.frame.size.height, SCREEN_WIDTH/3.5, 50)];
    addLabel.text = @"离你最近：";
    addLabel.textColor = [UIColor colorWithRed:244.0/255.0 green:37.0/255.0 blue:37.0/255.0 alpha:1];
    addLabel.font = [UIFont systemFontOfSize:17];
//    addLabel.backgroundColor = [UIColor yellowColor];
    addLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:addLabel];
    
    UILabel *detailAddLabel = [[UILabel alloc] initWithFrame:CGRectMake(addLabel.frame.origin.x + addLabel.frame.size.width + 3, addLabel.frame.origin.y, SCREEN_WIDTH/2, addLabel.frame.size.height)];
    detailAddLabel.text = @"好德-宁航店";
    detailAddLabel.textColor = [UIColor colorWithRed:244.0/255.0 green:37.0/255.0 blue:37.0/255.0 alpha:1];
    detailAddLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:detailAddLabel];
    
}

- (void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag == 1003) {
        return 2;
    }
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 1001) {
        return 1;
    }
    if (tableView.tag == 1003 && section == 0) {
        return 1;
    }
    if (tableView.tag == 1003 && section == 1) {
        return 7;
    }
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1001) {
            static NSString *cellIdentifier = @"cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                cell.textLabel.text = @"长宁区";
                cell.textLabel.font = [UIFont systemFontOfSize:15];
                cell.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:205.0 / 255.0 blue:0 / 255.0 alpha:1];
                cell.textLabel.textColor = [UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

            }
            
                  return cell;

    }
    
    if (tableView.tag == 1002) {
        static NSString *cellIdentifier = @"cellDetail";
        UITableViewCell *cellDetail = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cellDetail == nil) {
            cellDetail = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            
            UIImageView *addressImageView = [[UIImageView alloc] initWithFrame:CGRectMake(cellDetail.frame.size.width/2, 2 * cellDetail.frame.size.height/3, 15, 15)];
            //        addressImageView.backgroundColor = [UIColor yellowColor];
            UIImage *addressImage = [UIImage imageNamed:@"address.png"];
            addressImageView.image = addressImage;
            addressImageView.contentMode = UIViewContentModeScaleAspectFit;
            [cellDetail addSubview:addressImageView];
            
            UIButton *addressButton = [UIButton buttonWithType:UIButtonTypeCustom];
            addressButton.frame = CGRectMake(addressImageView.frame.origin.x + addressImageView.frame.size.width , addressImageView.frame.origin.y, SCREEN_WIDTH/5, addressImageView.frame.size.height);
            //        addressButton.backgroundColor = [UIColor yellowColor];
            [addressButton setTitleColor:[UIColor colorWithRed:244.0/255.0 green:37.0/255.0 blue:37.0/255.0 alpha:1] forState:UIControlStateNormal];
            [addressButton setTitle:@"进入地图" forState:UIControlStateNormal];
            addressButton.titleLabel.font = [UIFont systemFontOfSize:12];
            [addressButton addTarget:self action:@selector(mapButtonAction) forControlEvents:UIControlEventTouchUpInside];
            [cellDetail addSubview:addressButton];
            
            UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellDetail.frame.size.width/2, cellDetail.frame.size.height/2)];
            addLabel.text = @"可德便利店";
            //            addLabel.backgroundColor = [UIColor redColor];
            [cellDetail addSubview:addLabel];
            
            UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, addLabel.frame.origin.y + addLabel.frame.size.height , addLabel.frame.size.width, addLabel.frame.size.height)];
            detailLabel.text = @"上海市长宁区发法华镇路503号(近香花桥路)";
            detailLabel.font = [UIFont systemFontOfSize:13];
            
            CGRect detailFrame = detailLabel.frame;
            detailFrame.size.height = [[self class] heightForAddress:detailLabel.text];
            detailLabel.frame = detailFrame;
            detailLabel.numberOfLines = 0;
            [cellDetail addSubview:detailLabel];
            
            UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(addressImageView.frame.origin.x, addressImageView.frame.origin.y + addressImageView.frame.size.height + 3, addressImageView.frame.size.width + addressButton.frame.size.width, addressImageView.frame.size.height)];
            distanceLabel.text = @"距离3km";
            distanceLabel.font = [UIFont systemFontOfSize:12];
            distanceLabel.textAlignment = NSTextAlignmentCenter;
            distanceLabel.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
            [cellDetail addSubview:distanceLabel];
            
            
            if (indexPath.row % 2 == 0) {
                cellDetail.backgroundColor = [UIColor whiteColor];
            }
            else
            {
                cellDetail.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
            }
        }
        cellDetail.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cellDetail;
        
    }
    if (tableView.tag == 1003 && indexPath.section == 0) {
        static NSString *cellSection = @"cellSelect";
        UITableViewCell *cellselect = [tableView dequeueReusableCellWithIdentifier:cellSection];
        if (cellselect == nil) {
            cellselect = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellSection];
            cellselect.textLabel.text = @"娄山关路";
            cellselect.selectionStyle = UITableViewCellSelectionStyleNone;
            cellselect.backgroundColor = [UIColor colorWithRed:109.0/255.0 green:77.0 / 255.0 blue:69 / 255.0 alpha:1];
            cellselect.textLabel.text = @"选择商圈";
            cellselect.textLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:205.0 / 255.0 blue:0 / 255.0 alpha:1];
            cellselect.textLabel.textAlignment = NSTextAlignmentCenter;
            
                }
        return cellselect;

    }
    else
    {
        static NSString *cellAlert = @"cellTableView";
        UITableViewCell *cellTableView = [tableView dequeueReusableCellWithIdentifier:cellAlert];
        if (cellTableView == nil) {
            cellTableView = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellAlert];
            cellTableView.textLabel.text = @"娄山关路";
            cellTableView.selectionStyle = UITableViewCellSelectionStyleNone;
            cellTableView.textLabel.font = [UIFont systemFontOfSize:15];
            if (indexPath.row % 2 == 0) {
                cellTableView.backgroundColor = [UIColor whiteColor];
            }
            else
            {
                cellTableView.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
            }
            
        }
        return cellTableView;

    }

    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1001) {
        return 50;
    }
    if (tableView.tag == 1003 && indexPath.section == 0) {
        return 40;
    }
    if (tableView.tag == 1003 && indexPath.section == 1) {
        return 50;
    }
    return SCREEN_HEIGHT/7;
}
- (void)mapButtonAction
{
    NSLog(@"进入地图");
    MapViewController *mapVC = [[MapViewController alloc] init];
    [self.navigationController pushViewController:mapVC animated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)selectButtonAction
{
    NSLog(@"可选商圈");
    self.myImageView.hidden = NO;
}

- (void)headTapAction
{
    NSLog(@"选择");
    self.myImageView.hidden = NO;
}
+ (CGFloat)heightForAddress:(NSString *)address
{
    CGSize size = CGSizeMake((SCREEN_WIDTH - SCREEN_WIDTH / 4 - 1)/2, 1000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName];
    CGRect frame = [address boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1003) {
        self.myImageView.hidden = YES;
    }

}

- (void)tapAction
{
    self.myImageView.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
